package com.monsky.power.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import com.monsky.power.mobile.MobileException;
import com.monsky.power.service.mobile.MobileService;

public class SocketWaiter implements Runnable{

	private Socket client ;
	
	public SocketWaiter(Socket client){
		this.client = client;
		Thread thread = new Thread(this);
		thread.start();
	}
	
	@Override
	public void run() {
		try {
			InputStream is = client.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
			String message = br.readLine();
			System.out.println(message);
			String[] messages = message.split(":");
			String userid = messages[0];
			String content = messages[1];
			System.out.println("userid:"+userid+"   message:"+content);
			MobileService mobileService = new MobileService();
			try {
				mobileService.insertMessage(userid, content);
			} catch (MobileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
